<template>
    <view class="jk-table-container">
        <scroll-view scroll-x="true" class="header-data-item" :scroll-left="scrollLeft">
            <view class="data-item">
                <view
                    v-for="(headerItem, headerIndex) in tableHeader"
                    :key="headerIndex"
                    :class="[
                    'data-item-cell',
                    headerItem.width || 'flex-1'
                ]"
                    :style="{ width: headerItem.width ? headerItem.width : 'auto' }"
                >
                    <view class="data-item-cell-inner" :style="{justifyContent: setCellAlign(headerItem)}" v-if="headerItem.type && headerItem.type === 'seq'">序号</view>
                    <view class="data-item-cell-inner" :style="{justifyContent: setCellAlign(headerItem)}" v-else >{{ headerItem.title }}</view>
                </view>
            </view>
        </scroll-view>
        <scroll-view scroll-y="true" scroll-x="true" style="height: 240px;" @scroll="onBodyScroll">
            <view
                v-for="(dataItem, dataIndex) in tableData"
                :key="dataIndex"
                class="data-item"
                style=""
            >
                <view
                    v-for="(headerItem, headerIndex) in tableHeader"
                    :key="headerIndex"
                    :class="[
                        'data-item-cell',
                        headerItem.width || 'flex-1'
                    ]"
                    :style="{ width: headerItem.width ? headerItem.width : 'auto' }"
                >
                    <view class="data-item-cell-inner" :style="{justifyContent: setCellAlign(headerItem)}" v-if="headerItem.type && headerItem.type === 'seq'">{{ dataIndex + 1 }}</view>
                    <view class="data-item-cell-inner" :style="{justifyContent: setCellAlign(headerItem)}" v-else>{{ dataItem[headerItem.key] }}</view>
                </view>
            </view>
        </scroll-view>
    </view>
</template>

<script>
  export default {
    name: 'JkTableList',
    props: {
      height: {
        type: [Number], // px
        default: 0
      },
      tableHeader: {
        type: Array,
        default() {
          return [
              // { type: 'seq', title: '序号', width: '60px' },
              // { key: 'machineCode', title: '机台', width: '' },
              // { key: 'runningEfficiency', title: '效率', width: '' },
          ]
        }
      },
      tableData: {
        type: Array,
        default() {
          return []
        }
      },
      stripe: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        columnList: [],
        showPopup: false,
          scrollLeft: 0
      };
    },
    methods: {
        onBodyScroll(e) {
            console.log('滚动：', e.detail.scrollLeft, this.scrollLeft)
            if (e.detail.scrollLeft !== this.scrollLeft) {
                this.scrollLeft = e.detail.scrollLeft;
            }
        },
        setCellAlign(headerItem) {
            let str = headerItem.align === 'center' ? 'center' : headerItem.align === 'right' ? 'flex-end' : 'flex-start';
            return str;
        },
      onRow(e) {
        this.$emit('row', e);
      },
      onPull() {
        this.$emit('pull');
      },
      pullLoading(done) {
        done();
      },
      onAllSelection(e, arr) {
        this.$emit('checked', arr);
      },
      toggleAllSelection(e, arr) {
        this.$emit('checked', arr);
      }
    },
    watch: {
      tableHeader: {
        handler(newVal) {
          this.columnList = newVal.map(x => {
            return x;
          });
        },
        deep: true,
        immediate: true
      }
    }
  }
</script>
<style scoped lang="scss">
.jk-table-container {
    border: solid 1rpx #e8eaec;
    border-radius: 14rpx;
    overflow: hidden;
    .header-data-item {
        background: #f8f8f9;
        position: sticky;
    }
    .data-item {
        height: 68rpx;
        display: flex;
        align-items: center;
        font-size: 28rpx;
        .data-item-cell {
            height: 100%;
            flex-shrink: 0;
            border-right: solid 1rpx #e8eaec;
            border-bottom: solid 1rpx #e8eaec;
            .data-item-cell-inner {
                display: flex;
                align-items: center;
                height: 100%;
                padding: 4rpx 8rpx;
                box-sizing: border-box;
                word-break: break-all;
            }
        }
        .flex-1 {
            flex: 1;
        }
    }
}
</style>
